home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------------------------
-
- dragutil.c
-
- This reusable module contains miscellaneous Drag Manager utility routines.
-
- Copyright © 1994, Northwestern University.
-
- ----------------------------------------------------------------------------*/
-
- #include <Folders.h>
- #include <Drag.h>
-
- #include "def.h"
- #include "dragutil.h"
- #include "memutil.h"
- #include "teutil.h"
- #include "drawutil.h"
-
-
-
- /*----------------------------------------------------------------------------
- GetDropLocationDirectory
-
- Given an 'alis' drop location AEDesc, return the volume reference
- number and directory ID of the directory.
-
- Entry: dropLocation = pointer to 'alis' AEDesc record.
-
- Exit: function result = error code.
- *volumeID = volume reference number.
- *directoryID = directory ID.
-
- From Apple "HFS Drag Sample" sample code.
- ----------------------------------------------------------------------------*/
-
- OSErr GetDropLocationDirectory (AEDesc *dropLocation, short *volumeID,
- long *directoryID)
- {
- OSErr err = noErr;
- AEDesc targetDescriptor;
- FSSpec targetLocation;
- CInfoPBRec getTargetInfo;
-
- err = AECoerceDesc(dropLocation, typeFSS, &targetDescriptor);
- if (err != noErr) return err;
-
- BlockMoveData(*targetDescriptor.dataHandle, &targetLocation, sizeof(FSSpec));
-
- err = AEDisposeDesc(&targetDescriptor);
- if (err != noErr) return err;
-
- getTargetInfo.dirInfo.ioNamePtr = targetLocation.name;
- getTargetInfo.dirInfo.ioVRefNum = targetLocation.vRefNum;
- getTargetInfo.dirInfo.ioFDirIndex = 0;
- getTargetInfo.dirInfo.ioDrDirID = targetLocation.parID;
-
- err = PBGetCatInfoSync(&getTargetInfo);
- if (err != noErr) return err;
-
- *directoryID = getTargetInfo.dirInfo.ioDrDirID;
- *volumeID = targetLocation.vRefNum;
- return noErr;
- }
-
-
-
- /*----------------------------------------------------------------------------
- DragTargetWasTrash
-
- Check to see if the target of a drag and drop was the Finder trashcan.
-
- Entry: dragRef = drag reference.
-
- Exit: function result = true if target was trash.
- ----------------------------------------------------------------------------*/
-
- Boolean DragTargetWasTrash (DragReference dragRef)
- {
- AEDesc dropLocation;
- OSErr err = noErr;
- long dropDirID, trashDirID;
- short dropVRefNum, trashVRefNum;
-
- err = GetDropLocation(dragRef, &dropLocation);
- if (err != noErr) return false;
-
- if (dropLocation.descriptorType == typeNull) goto exit;
-
- err = GetDropLocationDirectory(&dropLocation, &dropVRefNum, &dropDirID);
- if (err != noErr) goto exit;
-
- err = FindFolder(dropVRefNum, kTrashFolderType, false, &trashVRefNum,
- &trashDirID);
- if (err != noErr) goto exit;
-
- if (dropVRefNum != trashVRefNum || dropDirID != trashDirID) goto exit;
-
- AEDisposeDesc(&dropLocation);
- return true;
-
- exit:
-
- AEDisposeDesc(&dropLocation);
- return false;
- }
-
-
-
- /*----------------------------------------------------------------------------
- MyGetFlavorDataHandle
-
- Get flavor data.
-
- Entry: dragRef = drag reference.
- itemRef = item reference.
- theType = flavor type.
-
- Exit: function result = error code.
- *flavorData = handle to flavor data.
- ----------------------------------------------------------------------------*/
-
- OSErr MyGetFlavorDataHandle (DragReference dragRef, ItemReference itemRef,
- FlavorType theType, Handle *flavorData)
- {
- OSErr err = noErr;
- Handle h = nil;
- Size len;
-
- err = GetFlavorDataSize(dragRef, itemRef, theType, &len);
- if (err != noErr) goto exit;
-
- err = MyNewHandle(len, &h);
- if (err != noErr) goto exit;
-
- MyHLock(h);
- err = GetFlavorData(dragRef, itemRef, theType, *h, &len, 0);
- if (err != noErr) goto exit;
- MyHUnlock(h);
-
- *flavorData = h;
- return noErr;
-
- exit:
-
- MyDisposeHandle(h);
- return err;
- }
-
-
-
- /*----------------------------------------------------------------------------
- DragText
-
- Drag selected text.
-
- Entry: ev = pointer to mouse down event record.
- where = location of mouse down event in local coords.
- theTE = handle to TextEdit record.
-
- Exit: function result = error code.
- *dragged =
- true if text was dragged.
- false if mouse down was not over text selection, or
- user did not move the mouse before releasing the
- mouse button.
- *trashed = true if text was dragged to trash.
- ----------------------------------------------------------------------------*/
-
- OSErr DragText (EventRecord *ev, Point where, TEHandle theTE,
- Boolean *dragged, Boolean *trashed)
- {
- DragReference dragRef;
- OSErr err = noErr;
- Boolean haveDragRef = false;
- RgnHandle dragRgn = nil;
- Handle hText;
- short selStart, selEnd;
- char state;
-
- *dragged = false;
- *trashed = false;
- if (!PtInTEHiliteRgn(where, theTE)) return noErr;
- if (!WaitMouseMoved(ev->where)) return noErr;
- *dragged = true;
-
- hText = (**theTE).hText;
- selStart = (**theTE).selStart;
- selEnd = (**theTE).selEnd;
-
- err = NewDrag(&dragRef);
- if (err != noErr) goto exit;
- haveDragRef = true;
- state = MyHGetState(hText);
- MyHLock(hText);
- err = AddDragItemFlavor(dragRef, 1, 'TEXT', *hText + selStart, selEnd - selStart, 0);
- MyHSetState(hText, state);
- if (err != noErr) goto exit;
- dragRgn = NewRgn();
- err = TEGetHiliteRgn(dragRgn, theTE);
- if (err != noErr) goto exit;
- LocalToGlobalRgn(dragRgn);
- OutlineRegion(dragRgn);
- err = TrackDrag(dragRef, ev, dragRgn);
- if (err != noErr && err != userCanceledErr) goto exit;
- *trashed = DragTargetWasTrash(dragRef);
- DisposeRgn(dragRgn);
- DisposeDrag(dragRef);
- return noErr;
-
- exit:
-
- if (haveDragRef) DisposeDrag(dragRef);
- if (dragRgn != nil) DisposeRgn(dragRgn);
- return err;
- }